﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ApiDoc
{
    partial class ApiDocumentationManager
    {
        /// <summary>
        /// Returns the AssemblyDocumentation for <paramref name="assembly"/>.
        /// </summary>
        /// <remarks>
        /// If <paramref name="assembly"/> is not already part of the documentation managed by this manager
        /// it will be added with AddAssembly() before generating the documentation for <paramref name="assembly"/>.
        /// </remarks>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public AssemblyDocumentation GetAssemblyDocumentation(Assembly assembly)
        {
            string name = assembly.ManifestModule.Name;

            // 

            // Check the cache.
            if (m_assembliesCache.ContainsKey(name)) {
                return m_assembliesCache[name];
            }

            // Cache the result.
            var ret = new AssemblyDocumentation(assembly, this);
            m_assembliesCache.Add(name, ret);

            return ret;
        }

        /// <summary>
        /// Returns the AssemblyDocumentation for <paramref name="cref"/>.
        /// </summary>
        /// <remarks>
        /// If <paramref name="assembly"/> is not already part of the documentation managed by this manager
        /// it will be added with AddAssembly() before generating the documentation for <paramref name="assembly"/>.
        /// </remarks>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public AssemblyDocumentation GetAssemblyDocumentation(string cref)
        {
            // If we have it in the cache then use it.
            if (m_assembliesCache.ContainsKey(cref)) {
                return m_assembliesCache[cref];
            }

            var assembly = m_assemblies.FirstOrDefault(item => item.ManifestModule.Name == cref);
            if (assembly == null) {
                return null; ;
            }

            // Cache the result.
            var ret = new AssemblyDocumentation(assembly, this);
            m_assembliesCache.Add(assembly.ManifestModule.Name, ret);

            return ret;
        }

        private Dictionary<string, AssemblyDocumentation> m_assembliesCache = new Dictionary<string, AssemblyDocumentation>();
    }
}
